home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / test / CubeView.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-10  |  4.2 KB  |  170 lines

  1. //
  2. // "$Id: CubeView.cxx,v 1.4 1999/03/10 16:40:19 mike Exp $"
  3. //
  4. // CubeView class implementation for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. #include "CubeView.h"
  27. #include <math.h>
  28.  
  29.  
  30. #if HAVE_GL
  31. CubeView::CubeView(int x,int y,int w,int h,const char *l)
  32.             : Fl_Gl_Window(x,y,w,h,l)
  33. #else
  34. CubeView::CubeView(int x,int y,int w,int h,const char *l)
  35.             : Fl_Box(x,y,w,h,l)
  36. #endif /* HAVE_GL */
  37. {
  38.     vAng = 0.0;
  39.     hAng=0.0;
  40.     size=10.0;
  41.     
  42.     /* The cube definition. These are the vertices of a unit cube
  43.      * centered on the origin.*/
  44.     
  45.     boxv0[0] = -0.5; boxv0[1] = -0.5; boxv0[2] = -0.5;
  46.     boxv1[0] =  0.5; boxv1[1] = -0.5; boxv1[2] = -0.5;
  47.     boxv2[0] =  0.5; boxv2[1] =  0.5; boxv2[2] = -0.5;
  48.     boxv3[0] = -0.5; boxv3[1] =  0.5; boxv3[2] = -0.5;
  49.     boxv4[0] = -0.5; boxv4[1] = -0.5; boxv4[2] =  0.5;
  50.     boxv5[0] =  0.5; boxv5[1] = -0.5; boxv5[2] =  0.5;
  51.     boxv6[0] =  0.5; boxv6[1] =  0.5; boxv6[2] =  0.5;
  52.     boxv7[0] = -0.5; boxv7[1] =  0.5; boxv7[2] =  0.5;
  53.  
  54. #if !HAVE_GL
  55.     label("OpenGL is required for this demo to operate.");
  56.     align(FL_ALIGN_WRAP | FL_ALIGN_INSIDE);
  57. #endif /* !HAVE_GL */
  58. };
  59.  
  60. #if HAVE_GL
  61. void CubeView::drawCube() {
  62. /* Draw a colored cube */
  63. #define ALPHA 0.5
  64.     glShadeModel(GL_FLAT);
  65.  
  66.     glBegin(GL_QUADS);
  67.       glColor4f(0.0, 0.0, 1.0, ALPHA);
  68.       glVertex3fv(boxv0);
  69.       glVertex3fv(boxv1);
  70.       glVertex3fv(boxv2);
  71.       glVertex3fv(boxv3);
  72.  
  73.       glColor4f(1.0, 1.0, 0.0, ALPHA);
  74.       glVertex3fv(boxv0);
  75.       glVertex3fv(boxv4);
  76.       glVertex3fv(boxv5);
  77.       glVertex3fv(boxv1);
  78.  
  79.       glColor4f(0.0, 1.0, 1.0, ALPHA);
  80.       glVertex3fv(boxv2);
  81.       glVertex3fv(boxv6);
  82.       glVertex3fv(boxv7);
  83.       glVertex3fv(boxv3);
  84.  
  85.       glColor4f(1.0, 0.0, 0.0, ALPHA);
  86.       glVertex3fv(boxv4);
  87.       glVertex3fv(boxv5);
  88.       glVertex3fv(boxv6);
  89.       glVertex3fv(boxv7);
  90.  
  91.       glColor4f(1.0, 0.0, 1.0, ALPHA);
  92.       glVertex3fv(boxv0);
  93.       glVertex3fv(boxv3);
  94.       glVertex3fv(boxv7);
  95.       glVertex3fv(boxv4);
  96.  
  97.       glColor4f(0.0, 1.0, 0.0, ALPHA);
  98.       glVertex3fv(boxv1);
  99.       glVertex3fv(boxv5);
  100.       glVertex3fv(boxv6);
  101.       glVertex3fv(boxv2);
  102.     glEnd();
  103.  
  104.     glColor3f(1.0, 1.0, 1.0);
  105.     glBegin(GL_LINES);
  106.       glVertex3fv(boxv0);
  107.       glVertex3fv(boxv1);
  108.  
  109.       glVertex3fv(boxv1);
  110.       glVertex3fv(boxv2);
  111.  
  112.       glVertex3fv(boxv2);
  113.       glVertex3fv(boxv3);
  114.  
  115.       glVertex3fv(boxv3);
  116.       glVertex3fv(boxv0);
  117.  
  118.       glVertex3fv(boxv4);
  119.       glVertex3fv(boxv5);
  120.  
  121.       glVertex3fv(boxv5);
  122.       glVertex3fv(boxv6);
  123.  
  124.       glVertex3fv(boxv6);
  125.       glVertex3fv(boxv7);
  126.  
  127.       glVertex3fv(boxv7);
  128.       glVertex3fv(boxv4);
  129.  
  130.       glVertex3fv(boxv0);
  131.       glVertex3fv(boxv4);
  132.  
  133.       glVertex3fv(boxv1);
  134.       glVertex3fv(boxv5);
  135.  
  136.       glVertex3fv(boxv2);
  137.       glVertex3fv(boxv6);
  138.  
  139.       glVertex3fv(boxv3);
  140.       glVertex3fv(boxv7);
  141.     glEnd();
  142. };//drawCube
  143.  
  144. void CubeView::draw() {
  145.     if (!valid()) {
  146.         glLoadIdentity();
  147.         glViewport(0,0,w(),h());
  148.         glOrtho(-10,10,-10,10,-20000,10000);
  149.         glEnable(GL_BLEND);
  150.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  151.     }
  152.  
  153.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  154.  
  155.     glPushMatrix();
  156.  
  157.     glTranslatef(xshift, yshift, 0);
  158.     glRotatef(hAng,0,1,0); glRotatef(vAng,1,0,0);
  159.     glScalef(float(size),float(size),float(size));
  160.  
  161.     drawCube();
  162.     
  163.     glPopMatrix();
  164. };
  165. #endif /* HAVE_GL */
  166.  
  167. //
  168. // End of "$Id: CubeView.cxx,v 1.4 1999/03/10 16:40:19 mike Exp $".
  169. //
  170.